program build_index
	parmest, fast
	keep if regexm(parm, "`1'")
	gen `1' = regexs(0) if regexm(parm, "[0-9]+")
	destring `1', replace
	keep `1' estimate
	ren estimate index_new_`1'
end

*======================== Index for all properties =======================

use "${datadir}\landreg_selected_no_Wales", replace

reg lprice i.datem, absorb(unit_id)	
preserve
	build_index datem
	ren index_new_datem index_eng_datem
	save "${datadir}\index_eng_repeat_all", replace
restore




*======================== Index for new properties ======================
use "${datadir}\landreg_selected_no_Wales_prevnew", replace

duplicates tag unit_id, gen(flag)
drop if flag < 1

bysort unit_id (dated): gen newcount = _n
drop if newcount > 2


*--- National index
reg lprice i.datem, absorb(unit_id)
preserve
	build_index datem
	save "${datadir}\index_eng_prevnew", replace
restore


*--- Local authorities

levelsof localauthoritycode, local(las)
foreach x of local las {
	di "*************************`x'**********************************"

	reg lprice new lease i.dateq if localauthoritycode == "`x'", absorb(unit_id)
				
	
	preserve	
		build_index dateq
		gen laua = "`x'"
		
		capture append using "${datadir}\index_la_prevnew"
		sa "${datadir}\index_la_prevnew", replace
	restore
}

*--- Prepare appreciation measure 
use "${datadir}\index_la_prevnew", replace

encode laua, gen(coden)
xtset coden dateq

gen appreciation2 = index/l8.index -1
gen appreciation3 = index/l12.index -1
gen appreciation5 = index/l20.index -1

save "${datadir}\la_prevnew_appreciation", replace
